package com.ccb.sc.common.modules.member.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccb.sc.common.modules.member.entity.Member;
import com.ccb.sc.common.modules.member.mapper.MemberMapper;
import com.ccb.sc.common.util.PasswordUtil;
import org.springframework.stereotype.Service;

/**
 * @author ken
 * @version 1.0
 * @date 2022-07-31 21:24
 */
@Service
public class MemberService extends ServiceImpl<MemberMapper, Member> {

    public Member findByPhone(String phone) {
        return this.getOne(new QueryWrapper<Member>().lambda().eq(Member::getPhone, phone).last("limit 1"));
    }

    public boolean updatePassword(Long memberId, String password) {
        Member member = this.getById(memberId);
        String salt = PasswordUtil.generateSalt();
        String encodePassword = PasswordUtil.encodePassword(password, salt);
        member.setVersion(member.getVersion());
        member.setSalt(salt);
        member.setPassword(encodePassword);
        return this.updateById(member);
    }
}
